package org.jeecg.modules.fund.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.fund.entity.FundLibrary;
import org.jeecg.modules.fund.service.IFundLibraryService;
import org.jeecg.modules.fund.util.FundAllCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 定时更新所有的基金代码信息
 */
@Slf4j
@Component
public class FundAllCodeJob {
    @Autowired
    private IFundLibraryService fundLibraryService;
    /**
     * 每周一至五，16点执行一次，代码刷新任务
     * @throws InterruptedException
     */
    @Scheduled(cron = "0 0 16 * * MON-FRI")
    public void execute() throws InterruptedException {
        // 线程池处理，数据超过2万条
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        // 获取数据
        JSONArray jsonArray = FundAllCodeUtil.getAllFund();
        // 遍历处理
        for (Object arr : jsonArray) {
            // 格式化处理
            JSONArray item = JSON.parseArray(arr.toString());
            // 基金代码
            String code = item.getString(0);
            // 简写
            String engPreName= item.getString(1);
            // 名称
            String name = item.getString(2);
            // 类型
            String type = item.getString(3);
            // 全拼
            String engAllName = item.getString(4);
            // 处理数据
            executorService.submit(() -> {
                // 类型处理
                String[] typeArr = type.split("-");
                // 保存并更新
                fundLibraryService.saveOrUpdate(new FundLibrary()
                        .setId("fund-" + code)
                        .setCode(code)
                        .setEngAllName(engAllName)
                        .setEngPreName(engPreName)
                        .setName(name)
                        .setType(typeArr[0])
                        .setSubType(typeArr[1]));
            });
        }
        executorService.shutdown();
    }
}
